/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.cloudfarming.client.planning.tactical;

import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.beans.IntrospectionException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import nl.cloudfarming.client.area.field.PartFieldLayer;
import nl.cloudfarming.client.geoviewer.LayerNode;
import nl.cloudfarming.client.model.ProductionUnit;
import nl.cloudfarming.client.planning.tactical.wizard.common.ActionTypeAction;
import org.openide.nodes.Children;
import org.openide.util.NbBundle;
import org.openide.util.lookup.Lookups;
import nl.cloudfarming.client.logging.AppLogFactory;
import nl.cloudfarming.client.logging.AppLogger;
import nl.cloudfarming.client.model.Surface;

/**
 *
 * @author Timon Veenstra
 */
public class ProductionUnitNode extends LayerNode {

    private static final AppLogger LOG = AppLogFactory.getLogger(Surface.class);
    private ProductionUnit productionUnit;

    public ProductionUnitNode(PartFieldLayer layer, ProductionUnit productionUnit) throws IntrospectionException {
        super(layer, Children.create(new PartFieldNodeFactory(productionUnit, layer), true), Lookups.fixed(productionUnit));
        this.productionUnit = productionUnit;
        productionUnit.addPropertyChangeListener(ProductionUnit.PROP_NAME, new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                setName();
            }
        });

        productionUnit.addPropertyChangeListener(ProductionUnit.PROP_CROP, new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                setName();
            }
        });

        this.productionUnit = productionUnit;
        setName();


    }

    private void setName() {
        if (productionUnit.getCrop() == null || productionUnit.getCrop().isEmpty()) {
            setName(productionUnit.getName());
        } else {
            setName(productionUnit.getName() + " (" + productionUnit.getCrop() + ")");
        }

    }

    @Override
    public Action[] getActions(boolean context) {
        if (productionUnit.getPartFields().size() > 0) {
            return new Action[]{new ActionTypeAction()};
        }
        return null;
        
    }
     
     
     /**
      * Action to open the actionViewer window. In this window the actions which are planned on a ProductionUnit are displayed.
      */
     public class openActionViewerAction extends AbstractAction {
         
        public openActionViewerAction() {
            super(NbBundle.getMessage(openActionViewerAction.class, "actionviewertopcomponent.open.viewer.action.title"));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            ActionViewerTopcomponent actionViewerTopcomponent = ActionViewerTopcomponent.findInstance();
            if (null != actionViewerTopcomponent) {
                actionViewerTopcomponent.updateTable();
                actionViewerTopcomponent.open();
            }
        }
    }

    /**
     * Method overriden from super. When ProductionUnit is dragged the node should be expanded otherwise the partFields are nog loaded and therefore not shown on the map.
     * @return
     * @throws IOException 
     */
    @Override
    public Transferable drag() throws IOException {
        getChildren().getNodes();
        return super.drag();
    }
}
